<template>
  <div class="bg">
    <div class="btn" @click="goBack">返 回</div>
    <div class="imgbox">
      <img src="~@/assets/images/idCardRead.png" alt="" />
      <div class="tip">
          请将身份证放置在读卡器上 
          <span v-if="!showBtn">{{msgTime}}...</span>
        <div class="refreshBtn">
          <el-button v-if="showBtn" type="success" plain icon="el-icon-refresh" @click="handleRefresh">重试</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "idCardLogin",
  data() {
    return {
      loginForm: {
        idCardNo: ""
      },
      timer: null,
      msgTime: 30,
      showBtn: false,
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  mounted() {
    this.getTime();
    this.getIdentity();
  },
  methods: {
    goBack() {
      this.loginForm.idCardNo = "530421199610162321";
      this.handleLogin();
      // this.$router.replace({
      //   path: '/login'
      // });
    },
    //采集身份证信息
    getIdentity() {
      axios
        .get("http://127.0.0.1:8091/api/identity?readtimeout=30000")
          .then((res) => {
            if (res.data.Result != 0) {
              this.$message({
                message: "读取失败，请重新刷身份证!",
                type: "error",
              });
            } else if (res.data.Data != "") {
              // this.$message({
              //   message: "身份证读取成功，登录中...",
              //   type: "success",
              // });
              this.loginForm.idCardNo = res.data.Data.PeopleIDCode;
              this.handleLogin();
            }
          }).catch((e) => {
          if(e) {
            this.$message({
              message: "设备连接失败，请检查设备后重试!",
              type: "error",
            });
            this.showBtn = true;
            clearInterval(this.timer);
            this.timer = null;
            this.msgTime = 30;
          }
        });
    },
    getTime() {
      this.timer = setInterval(() => {
        this.msgTime--;
        if (this.msgTime === 0) {
          this.showBtn = true;
          this.msgTime = 30;
          clearInterval(this.timer);
          this.timer = null;
        }
      }, 1000);
    },
    handleRefresh() {
      this.showBtn = false;
      this.getIdentity();
      this.getTime();
    },
    handleLogin() {
      this.$store.dispatch("Login", this.loginForm).then((res) => {
        this.$router.push({ path: this.redirect || "/home" });
      }).catch((error) => {});
    }
  },
  beforeDestroy() {
    this.showBtn = true;
    this.msgTime = 30;
    clearInterval(this.timer);
    this.timer = null;
  }
}
</script>

<style lang="scss" scoped>
.bg {
  width: 100%;
  height: 100%;
  background: url("~@/assets/images/bg.png");
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  .btn {
    width: 150px;
    height: 50px;
    cursor: pointer;
    color: #fff;
    font-size: 20px;
    font-weight: bold;
    line-height: 50px;
    padding-left: 70px;
    border: 1px solid #babfca;
    position: absolute;
    top: 80px;
    left: 120px;
    background: url("~@/assets/images/back.png") no-repeat;
    background-size: 30px auto;
    background-position: 20px center;
  }
  .imgbox {
    width: 500px;
    height: 300px;
    border-radius: 10px;
    background: #546fba;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    img {
      width: 60%;
    }
    .refreshBtn {
      margin-top: 15px;
      .el-button {
        border-color: #fff;
        color: #fff;
        background: transparent;
      }
    }
    .tip {
      width: 500px;
      font-size: 28px;
      color: #d7eafe;
      text-align: center;
      position: absolute;
      left: 0;
      bottom: -130px;
    }
  }
}

</style>